package com.weixin.paleituo.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.bean.TemplateDetailsBean;
import com.weixin.paleituo.util.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 样板间标签
 *
 * @author fengzhen
 * @version v1.0, 2018/5/7
 */
public class TagGridAdapter extends BaseAdapter {
    List<TemplateDetailsBean.TagsBean> mTags;

    public TagGridAdapter(List<TemplateDetailsBean.TagsBean> tags) {
        mTags = tags;
    }

    @Override
    public int getCount() {
        return mTags.size();
    }

    @Override
    public Object getItem(int position) {
        return mTags.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = UIUtils.inflate(parent.getContext(), R.layout.view_shared_template_decoration, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        TemplateDetailsBean.TagsBean tagInfo = mTags.get(position);
        holder.mTvViewSharedTemplateDecoration.setText(tagInfo.getTname());
        Glide.with(parent.getContext())
                .load(tagInfo.getUrl())
                .asBitmap()
                .error(R.mipmap.icon_shared_model_tv)
                .placeholder(R.mipmap.icon_shared_model_tv)
                .into(holder.mImgViewSharedTemplateDecoration);
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.img_view_shared_template_decoration)
        ImageView mImgViewSharedTemplateDecoration;
        @BindView(R.id.tv_view_shared_template_decoration)
        TextView mTvViewSharedTemplateDecoration;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
